//
//  FCSegmentView.m
//  FastCall
//
//  Created by Carlson Lee on 2017/12/8.
//  Copyright © 2017年 XiaoNiu Ltd. All rights reserved.
//

#import "FCSegmentView.h"

@interface FCSegmentView ()

@property (nonatomic, strong) UIScrollView* scroll;

@end

@implementation FCSegmentView

- (instancetype)init
{
    if(self = [super init]){
        _tagsWidth = 0;
        _tagsOffset = CGPointMake(20*kScaleX, 0);
        _lineSize = CGSizeMake(15, 4);
    }
    return self;
}

- (instancetype)initWithColor:(UIColor* )color selectColor:(UIColor* )sColor font:(UIFont* )font
{
    if(self = [super init]){
        _tagColor = color;
        _tagSelectColor = sColor;
        _tagFont = font;
        _tagsWidth = 0;
        _tagsOffset = CGPointMake(20*kScaleX, 0);
        _lineSize = CGSizeMake(15, 4);
        _selectScale = 1.0;
    }
    return self;
}

- (void)setTags:(NSArray* )tags atIndex:(NSInteger )index
{
    _index = index;
    __block NSMutableArray* mArr = [NSMutableArray array];
    _tagsWidth = 0;
    [tags enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.scroll addSubview:btn];
        [btn setTitleColor:_tagColor forState:UIControlStateNormal];
        [btn setTitleColor:_tagSelectColor forState:UIControlStateSelected];
        [btn.titleLabel setFont:_tagFont];
        btn.tag = idx;
        if([obj isKindOfClass:[NSString class]]){
            [btn setTitle:obj forState:UIControlStateNormal];
        }
        CGSize sz = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:_tagFont}];
        sz.width += _tagsOffset.x;
        btn.frame = (CGRect){{0, 0}, sz};
        _tagsWidth += sz.width;
        if(idx == _index){
            btn.selected = YES;
            btn.transform = CGAffineTransformMakeScale(_selectScale, _selectScale);
        }
        
        [btn addTarget:self action:@selector(btnSelect:) forControlEvents:UIControlEventTouchUpInside];
        [mArr addObject:btn];
    }];
    self.items = [mArr copy];
}

- (void)btnSelect:(UIButton* )btn
{
    [self selectWithTag:btn.tag];
}

- (void)selectWithTag:(NSInteger )tag
{
    UIButton* btn = self.items[tag];
    if(_index!=btn.tag){
        UIButton* lbtn = self.items[_index];
        lbtn.selected = NO;
        lbtn.transform = CGAffineTransformIdentity;
    }
    btn.selected = YES;
    btn.transform = CGAffineTransformMakeScale(_selectScale, _selectScale);
    if(!_isLineHidden){
        CGPoint pt = CGPointMake(btn.center.x, self.tagLine.center.y);
        self.tagLine.center = pt;
    }
    _index = btn.tag;
    
    if(_tagsWidth>kBounds.width){
        CGPoint p = [self.scroll convertPoint:btn.center toView:self];
        CGFloat f_x = fabsf(self.center.x-p.x);
        CGPoint f_l = self.scroll.contentOffset;
        CGFloat f_r = self.scroll.contentSize.width-f_l.x-CGRectGetWidth(self.frame);
        CGPoint offset = f_l;
        if(p.x<self.center.x){
            if(f_l.x>f_x){
                offset = CGPointMake(f_l.x-f_x, 0);
            }else{
                offset = CGPointMake(0, 0);
            }
        }else{
            if(f_r>f_x){
                offset = CGPointMake(f_l.x+f_x, 0);
            }else{
                offset = CGPointMake(f_l.x+f_r, 0);
            }
        }
        [self.scroll setContentOffset:offset animated:YES];
    }
    
    if(_delegate && [_delegate respondsToSelector:@selector(segmentSelectWithTag:)]){
        [_delegate segmentSelectWithTag:btn.tag];
    }
}

- (void)segmentScrollContentOffset:(CGFloat )offsetX{}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.scroll.frame = self.bounds;
    [self.scroll setContentInset:UIEdgeInsetsZero];
    if(!_isLineHidden)self.tagLine.bounds = CGRectMake(0, 0, _lineSize.width, _lineSize.height);
    __block CGFloat f_x = 0;
    CGFloat f_h = CGRectGetHeight(self.frame);

    if(_tagsWidth < kBounds.width){
        f_x = (kBounds.width-_tagsWidth)/2;
    }
    [self.items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton* btn = obj;
        CGRect rt = btn.frame;
        rt.origin.x = f_x;
        rt.origin.y = (kBounds.height-rt.size.height)/2;
        btn.frame = rt;
        f_x += rt.size.width;
        if(_index == idx && !_isLineHidden){
            CGPoint pt = btn.center;
            self.tagLine.center = CGPointMake(pt.x, f_h-_lineSize.height/2-_tagsOffset.y);
        }
    }];
    [self.scroll setContentSize:CGSizeMake(f_x, 0)];
}

- (void)setIsLineHidden:(BOOL)isLineHidden
{
    _isLineHidden = isLineHidden;
    [self.tagLine setHidden:_isLineHidden];
}

//
- (UIScrollView *)scroll
{
    if(!_scroll){
        _scroll = [[UIScrollView alloc]init];
        [self addSubview:_scroll];
       
        [_scroll setShowsHorizontalScrollIndicator:NO];
        [_scroll setBackgroundColor:[UIColor clearColor]];
    }
    return _scroll;
}

//标签标注线
- (UIView *)tagLine
{
    if(!_tagLine){
        _tagLine = [UIView new];
        [self.scroll addSubview:_tagLine];
        [_tagLine setBackgroundColor:_lineColor];
        [_tagLine.layer setCornerRadius:_lineSize.height/2];
    }
    return _tagLine;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
